//
//  QInvoiceRecordVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/2/20.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QInvoiceRecordVC.h"
#import "QInvoiceRecordCell.h"
#import "QInvoiceDetailVC.h"
#import "QInvoiceInfoModel.h"
#import "QInvoiceRecordDateCell.h"
@interface QInvoiceRecordVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation QInvoiceRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"开票记录";
    _dataArr =[NSMutableArray array];
    [self createUI];
    [self requestData];
}

- (void)createUI{
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    [self.tbView registerNib:[UINib nibWithNibName:@"QInvoiceRecordCell" bundle:nil] forCellReuseIdentifier:@"QInvoiceRecordCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QInvoiceRecordDateCell" bundle:nil] forCellReuseIdentifier:@"QInvoiceRecordDateCell"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0) {
        return 100.7;
    }
    QInvoiceRecordModel *model =_dataArr[indexPath.row];
    QInvoiceRecordModel *model1=_dataArr[indexPath.row - 1];
    if (![model1.month isEqualToString:model.month]) {
        return 100.7;
    }
    
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QInvoiceRecordModel *model =_dataArr[indexPath.row];

    if (indexPath.row == 0) {
        QInvoiceRecordDateCell *cell =[tableView dequeueReusableCellWithIdentifier:@"QInvoiceRecordDateCell"];
        cell.model =model;
        return cell;
    }
    QInvoiceRecordModel *model1=_dataArr[indexPath.row - 1];
    if (![model1.month isEqualToString:model.month]) {
        QInvoiceRecordDateCell *cell =[tableView dequeueReusableCellWithIdentifier:@"QInvoiceRecordDateCell"];
        cell.model =model;
        return cell;
    }else{
        QInvoiceRecordCell * cell= [tableView dequeueReusableCellWithIdentifier:@"QInvoiceRecordCell" forIndexPath:indexPath];

        cell.model =model;
        return cell;
    }


}


- (void)requestData{
    NSString *url = [QParamService urlWithAction:@"findInvoiceRecordList"];
    NSDictionary *para =[QParamService findInvoiceRecordList];
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility dismissNoDataViewInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [_dataArr removeAllObjects];
            //QInvoiceInfoModel *model =[QInvoiceInfoModel objectFromDict:basModel.items];
            
            _dataArr =(NSMutableArray *)[QInvoiceRecordModel objectArrFromDictArr:[basModel.items objectForKey:@"list"]];
            
            
            if(_dataArr.count == 0){
                [QViewUtility showNoDataViewInView:_tbView string:@""];
            }else{
                if(_dataArr.count < 10){
                    [_tbView.mj_footer noticeNothing];
                }
            }
            [_tbView reloadData];
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QInvoiceRecordModel *model =_dataArr[indexPath.row];
    QInvoiceDetailVC * vc =[[QInvoiceDetailVC alloc]init];
    vc.model=model;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        //_tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
